import { Big, MC, RoundingMode } from 'bigdecimal.js';
import { multiply100, numThousandthFilter, toPercent } from 'microconnect-common/utils/math';

const fnMap = {
  AMOUNT: numThousandthFilter,
  PERCENT: toPercent,
  NUMBER100: multiply100
};

export const formatter = (val: any, type: 'AMOUNT' | 'PERCENT' | 'NUMBER100') => {
  if (val === 0) return val;
  if (!val) return '-';
  return fnMap[type](val);
};

// 精准加法
export const accAdd = (num1: number, num2: number) => {
  // let r1, r2, m;
  // try {
  //   r1 = num1.toString().split('.')[1].length;
  // } catch (e) {
  //   r1 = 0;
  // }
  // try {
  //   r2 = num2.toString().split('.')[1].length;
  // } catch (e) {
  //   r2 = 0;
  // }
  // m = Math.pow(10, Math.max(r1, r2));
  // return (num1 * m + num2 * m) / m;
 const x = Big(num1);
 const y = Big(num2);
 const result = x.add(y).toString();
  return parseFloat(result);
};

// 精准减法
export const accSubtract = (num1: number, num2: number) => {
  /** let r1, r2, m, n;
  try {
    r1 = num1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = num2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  n = r1 >= r2 ? r1 : r2;
  return ((num1 * m - num2 * m) / m).toFixed(n);**/
  const x = Big(num1);
  const y = Big(num2);
  const resultStr =  x.subtract(y).toString();
  return parseFloat(resultStr);
};

// 精准乘法
export const accMul = (num1: number, num2: number) => {
   /** let m = 0,
    s1 = num1.toString(),
    s2 = num2.toString();
  try {
    m += s1.split('.')[1].length;
  } catch (e) {}
  try {
    m += s2.split('.')[1].length;
  } catch (e) {}
  return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);**/
  const x = Big(num1);
  const y = Big(num2);
  const result = x.multiply(y).toString();
  return parseFloat(result);
};

// 精准除法
export const accDiv = (num1: number, num2: number, length =4) => {
  /** let t1 = 0,
    t2 = 0,
    r1,
    r2;
  try {
    t1 = num1.toString().split('.')[1].length;
  } catch (e) {}
  try {
    t2 = num2.toString().split('.')[1].length;
  } catch (e) {}
  r1 = Number(num1.toString().replace('.', ''));
  r2 = Number(num2.toString().replace('.', ''));
  return accMul(r1 / r2, Math.pow(10, t2 - t1));**/
  const x = Big(num1);
  const y = Big(num2);
  const result =  x.divideWithMathContext(y, new MC(length, RoundingMode.UP)).toString();
  return parseFloat(result);
};

/**
 * 格式化ms时间，ms转s
 * @param num   {String, Number} 数值
 * @param unit  {Number} 小数位数
 * @return      {String} 返回字符串
 */
export const millisecond = (value: number): string => {
  if (isNaN(value) === true) return value + "";
  if (value === 0) {
    return "0";
  }

  // 判断是否在毫秒级别
  if (value < 1000) {
    return value.toFixed(0) + "ms";
  }

  // 转换为秒并判断是否在秒级别
  let seconds = value / 1000;
  if (seconds < 60) {
    return seconds.toFixed(2) + "s";
  }

  // 计算分钟数
  let minutes = value / 1000 / 60;
  if(minutes < 60) {
    return minutes.toFixed(2) + "min";
  }

  // 计算小时数
  let hour = value / 1000 / 60 / 60;
  return hour.toFixed(2) + "h";
};